package com.txby.platformApi.component.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.Constant;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformCore.entity.ProductDeviceInit;
import com.txby.platformCore.entity.SysPaper;

/**
 * 试卷
 */
@RestController
@RequestMapping("/sys/paper")
public class PaperController  extends CommonController {
	
	/**
	 * 所有试卷列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:paper:list")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="SELECT * FROM "+this.mBaseDao.tableName(SysPaper.class) ;
	/*	if(getUserId() != Constant.SUPER_ADMIN){
//			params.put("createUserId", getUserId());
			 join=getJoinSqlBracket();
			 sql+=" and name in "+getJoinSqlBracket();
		}*/
		String name=params.get("name")==null?"":params.get("name").toString();
		if(!ChkUtil.isEmptyAllObject(name)){
			if (name.contains("%") || name.contains("_")) {
				name = name.trim().replace("%", "\\%").replace("_", "\\_");
				name = "%" + name + "%";
				sql += " where name like #{pojnameic} escape '\\'";
				params.put("name", "%" + name.trim() + "%");
			} else {
				name = "%" + name.trim() + "%";
				sql += " where name like #{pojo.name} ";
				params.put("name", "%" + name.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by updateTime ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
 
	
	/**
	 * 用户信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:paper:info")
	public R info(@PathVariable("id") Long id){
		String sql="SELECT * FROM  "+this.mBaseDao.tableName(SysPaper.class)+" WHERE id_="+id+"" ;
		Map paper =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("paper", paper);
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@RequestMapping("/save")
	@RequiresPermissions("sys:paper:save")
	public R save(@RequestBody SysPaper paper){
		try{
		ValidatorUtils.validateEntity(paper);
		String sql="select * from "+this.mBaseDao.tableName(SysPaper.class)+" where name='"+paper.getName()+"'";
		SysPaper old=(SysPaper) this.mBaseDao.findBySqlUniqueObject(sql, SysPaper.class, null);
		 if(old!=null){
			 return R.ok().error("试卷名称重复");
		 }
		 paper.setCreater(getUserId().toString());
		 paper.setUpdater(getUserId().toString());
			this.mBaseDao.saveObject(paper);
		}catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@RequestMapping("/update")
	@RequiresPermissions("sys:paper:update")
	public R update(@RequestBody  SysPaper paper){
		try{
		ValidatorUtils.validateEntity(paper);
		   String sql="select * from "+this.mBaseDao.tableName(SysPaper.class)+" where name='"+paper.getName()+"' and id_!="+paper.getId_()+" ";
		SysPaper old=(SysPaper) this.mBaseDao.findBySqlUniqueObject(sql, SysPaper.class, null);
		 if(old!=null){
			 return R.ok().error("试卷名称重复");
		 }else{
			 old=(SysPaper) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(SysPaper.class)+" where   id_="+paper.getId_(), SysPaper.class, null);
			 this.mBaseDao.updateObject(paper);
		 }
		}catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:paper:delete")
	public R delete(@RequestBody Long[] ids){
		 for(Long id:ids){
		    	this.mBaseDao.executeSql(" delete from "+this.mBaseDao.tableName(SysPaper.class)+" where id_="+id+"", null);
		  }
			return R.ok();
	}
	
	/**
	 * 查询单选
	 */
	@RequestMapping("/querysingleList")
	public R querysingleList(){
		String sql="select topic text,id_ value from t_txby_bd_sysCusTopic where type=1 ";
		List querysingleList=this.mBaseDao.findBySql(sql, Map.class, null);
		return R.ok().put("singleList", querysingleList);
	}
	
	/**
	 * 查询多选
	 */
	@RequestMapping("/querymultiList")
	public R querymultiList(){
		String sql="select topic text,id_ value from t_txby_bd_sysCusTopic where type =2 ";
		List querymultiList=this.mBaseDao.findBySql(sql, Map.class, null);
		return R.ok().put("multiList", querymultiList);
	}
	
	/**
	 * 查询简答
	 */
	@RequestMapping("/querysaqList")
	public R querysaqList(){
		String sql="select topic text,id_ value from t_txby_bd_sysCusTopic where type =3 ";
		List querysaqList=this.mBaseDao.findBySql(sql, Map.class, null);
		return R.ok().put("saqList", querysaqList);
	}
}
